package com.ddddddl.widget.scrollTab;

import com.ddddddl.zhangben.R;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class ScrollTabContactAndClusterActivity extends ActivityGroup {
	private ScrollTab tab;
	private String defaultTab = null;
	private int defaultTabIndex = -1;
	private int lastOrientation = -1;
	protected Handler mHandler = new Handler();

	public void setDefaultTab(String tag) {
		defaultTab = tag;
		defaultTabIndex = -1;
	}

	public void setDefaultTab(int index) {
		defaultTab = null;
		defaultTabIndex = index;
	}

	@Override
	protected void onRestoreInstanceState(Bundle state) {
		super.onRestoreInstanceState(state);
		ensureTabHost();

		if (tab.getCurrentTab() == -1 || tab.getChildCount() == 0) {
			tab.setupChild();
		}

		String cur = state.getString("currentTab");
		if (cur != null) {
			tab.setCurrentTabByTag(cur, false);
		}
		if (tab.getCurrentTab() < 0) {
			if (defaultTab != null) {
				tab.setCurrentTabByTag(defaultTab, false);
			} else if (defaultTabIndex >= 0) {
				tab.setCurrentTab(defaultTabIndex, false);
			}
		}
	}

	@Override
	protected void onPostCreate(Bundle icicle) {
		super.onPostCreate(icicle);

		ensureTabHost();

		if (tab.getCurrentTab() == -1 || tab.getChildCount() == 0) {
			tab.setupChild();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		String currentTabTag = tab.getCurrentTabTag();
		if (currentTabTag != null) {
			outState.putString("currentTab", currentTabTag);
		}
	}

	@Override
	public void onContentChanged() {
		super.onContentChanged();
		tab = (ScrollTab) findViewById(android.R.id.tabhost);

		if (tab == null) {
			throw new RuntimeException(
					"Your content must have a TabHost whose id attribute is "
							+ "'android.R.id.tabhost'");
		}
		tab.setup(getLocalActivityManager());
	}

	private void ensureTabHost() {
		if (tab == null) {
			if (getScrollContentView() != null)
				this.setContentView(getScrollContentView());
			else if (getContentRes() != 0) {
				this.setContentView(getContentRes());
			}
		}
	}

	protected int getContentRes() {
		return R.layout.tab;
	}

	protected View getScrollContentView() {
		return null;
	}

	@Override
	protected void onChildTitleChanged(Activity childActivity,
			CharSequence title) {
		if (getLocalActivityManager().getCurrentActivity() == childActivity) {
			View tabView = tab.getCurrentTabView();
			if (tabView != null && tabView instanceof TextView) {
				((TextView) tabView).setText(title);
			}
		}
	}

	public ScrollTab getTabHost() {
		ensureTabHost();
		return tab;
	}

	public FlingTabWidget getTabWidget() {
		return tab.getTabWidget();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		tab.onScrollTabConfigurationChanged(newConfig);
		lastOrientation = getResources().getConfiguration().orientation;
	}

	@Override
	protected void onResume() {
		super.onResume();
//		Log.d("ScrollTabContactAndClusterActivity", String.format("%1$d:%1$d",
//				getParent().getResources().getConfiguration().orientation));
//
//		if (lastOrientation != -1
//				&& lastOrientation != getParent().getResources()
//						.getConfiguration().orientation) {
//			onConfigurationChanged(getParent().getResources()
//					.getConfiguration());
//		}
//		lastOrientation = getParent().getResources().getConfiguration().orientation;
//		tab.getCurrentView().requestFocus();
	}

}
